package com.missilike.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.missilike.biz.*;
import com.missilike.core.enums.MilPushType;
import com.missilike.core.service.IIMService;
import com.missilike.domain.request.MilPushForm;
import com.missilike.domain.response.MilAccountInfo;
import com.missilike.repository.entity.MilAccount;
import com.missilike.repository.entity.MilFriendApply;
import com.missilike.repository.entity.MilFriendRelationship;
import com.missilike.repository.entity.extend.MilFriendApplyExtend;
import com.missilike.repository.mapper.MilFriendApplyMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author pengpengli
 * @since 2018-12-19
 */
@Service
@Slf4j
public class MilFriendApplyServiceImpl extends ServiceImpl<MilFriendApplyMapper, MilFriendApply> implements IMilFriendApplyService {

    private final static int REQUST = 0;
    private final static int AGREE = 1;
    private final static int REFUSE = 2;

    private  IMilFriendRelationshipService friendRelationshipService;

    private  IMilAccountService accountService;

    private final IIMService iimService;

    private  IMilEquipmentService iMilEquipmentService;

    private final IMilPhotoService photoService;

    private final IMilFollowRelationshipService iMilFollowRelationshipService;

    @Autowired
    public MilFriendApplyServiceImpl(IIMService iimService, IMilPhotoService photoService,IMilFollowRelationshipService iMilFollowRelationshipService) {
        this.iimService = iimService;
        this.photoService = photoService;
        this.iMilFollowRelationshipService=iMilFollowRelationshipService;
    }



    @Override
    public MilFriendApply friendRequest(MilFriendApply apply) {
        if (apply.getApplyStatus() == null || apply.getApplyStatus() != REQUST) {
            apply.setApplyStatus(REQUST);
        }
        this.save(apply);
        //todo 推送
        MilPushForm milPushForm = new MilPushForm();
        milPushForm.setAccountId(apply.getFromAccountId());
        milPushForm.setFriendAccountId(apply.getToAccountId());
        milPushForm.setType(MilPushType.FRIEND_APPLICATION.getValue());
        iMilEquipmentService.publishMassage(milPushForm);
        return apply;
    }

    @Override
    public boolean friendRequestResponse(MilFriendApply friendApply) {
        //todo 确认多条记录处理方式
       MilFriendApply apply = this.getById(friendApply.getId());
        if (apply.getApplyStatus() == null) {
            return false;
        }

        MilPushForm milPushForm = new MilPushForm();
        milPushForm.setAccountId(apply.getFromAccountId());
        milPushForm.setFriendAccountId(apply.getToAccountId());
        milPushForm.setType(MilPushType.FRIEND_APPLICATION_RESPONSE.getValue());

        apply.setApplyStatus(friendApply.getApplyStatus());
        this.updateById(apply);
        if (apply.getApplyStatus() == AGREE) {
            MilAccountInfo toAccount = accountService.getAccount(apply.getToAccountId());
            MilFriendRelationship relationship = new MilFriendRelationship();
            relationship.setAccountId(apply.getFromAccountId());
            relationship.setFriendAccountId(apply.getToAccountId());
            relationship.setCreateTime(LocalDateTime.now());
            relationship.setFriendAccount(toAccount.getAccount());
            relationship.setFriendAccountNickName(toAccount.getNickName());
            if (toAccount.getFaceUrlId() != null) {
                relationship.setFriendAccountHeadId(toAccount.getFaceUrlId());
            }
            friendRelationshipService.addFriend(relationship);
            MilAccount fromAccount = accountService.getById(apply.getFromAccountId());
            iimService.addFriend(fromAccount.getAccount(), toAccount.getAccount());
            milPushForm.setApplyStatus(true);
            iMilFollowRelationshipService.addMutualConcern(milPushForm.getAccountId(),milPushForm.getFriendAccountId());
        }else if (apply.getApplyStatus() == REFUSE){
            milPushForm.setApplyStatus(false);
        }
        //todo 推送
        iMilEquipmentService.publishMassage(milPushForm);
        return true;
    }

    @Override
    public List<MilFriendApplyExtend> getApply(String toAccountId, Long lastId, Integer pageSize) {
        QueryWrapper<MilFriendApply> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MilFriendApply::getToAccountId, toAccountId);
        if(lastId != null && lastId>0){
            wrapper.lambda().lt(MilFriendApply::getId, lastId);
        }
        wrapper.lambda().orderByDesc(MilFriendApply::getCreateTime);
        wrapper.last(" limit " + pageSize);
        List<MilFriendApply> applyList =  baseMapper.selectList(wrapper);
        List<MilFriendApplyExtend> result = new ArrayList<>();
        for (MilFriendApply apply : applyList){
            MilFriendApplyExtend extend = new MilFriendApplyExtend();
            BeanUtils.copyProperties(apply,extend);
            MilAccount account = accountService.getById(apply.getFromAccountId());
            if(null != account){
                extend.setNickName(account.getNickName());
                if(account.getFaceUrlId() != null && account.getFaceUrlId() > 0){
                    extend.setFaceUrl(photoService.getPhotoUrl(account.getFaceUrlId()));
                }
            }
            result.add(extend);
        }

        return result;
    }

    @Override
    public boolean inApply(String fromId, String toId){
        QueryWrapper<MilFriendApply> applyQueryWrapper = new QueryWrapper<>();
        applyQueryWrapper.lambda().eq(MilFriendApply::getFromAccountId,fromId).eq(MilFriendApply::getToAccountId,toId).eq(MilFriendApply::getApplyStatus,0);
        return baseMapper.selectList(applyQueryWrapper).size() > 0;
    }

    @Override
    public void setFriendApplyService(MilAccountServiceImpl milAccountService) {
        this.accountService = milAccountService;
    }

    @Override
    public void setFriendRelationshipService(IMilFriendRelationshipService friendRelationshipService) {
        this.friendRelationshipService = friendRelationshipService;
    }

    @Override
    public void setEquipmentService(IMilEquipmentService iMilEquipmentService) {
        this.iMilEquipmentService = iMilEquipmentService;
    }
}
